package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.enums.DelFlagStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysUserChannel;
import com.ruoyi.system.mapper.SysUserChannelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysChannelMapper;
import com.ruoyi.system.domain.SysChannel;
import com.ruoyi.system.service.ISysChannelService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通道Service业务层处理
 *
 * @author Ericcc
 * @date 2021-02-12
 */
@Service
public class SysChannelServiceImpl implements ISysChannelService
{
    @Autowired
    private SysChannelMapper sysChannelMapper;

    @Autowired
    private SysUserChannelMapper sysUserChannelMapper;

    /**
     * 查询通道
     *
     * @param channelId 通道ID
     * @return 通道
     */
    @Override
    public SysChannel selectSysChannelById(Long channelId)
    {
        return sysChannelMapper.selectSysChannelById(channelId);
    }

    /**
     * 查询通道列表
     *
     * @param sysChannel 通道
     * @return 通道
     */
    @Override
    public List<SysChannel> selectSysChannelList(SysChannel sysChannel)
    {
        return sysChannelMapper.selectSysChannelList(sysChannel);
    }

    /**
     * 新增通道
     *
     * @param sysChannel 通道
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysChannel(SysChannel sysChannel)
    {
        int number = 300;
        SysChannel checkNumber = sysChannelMapper.selectOneSysChannel();
        if (checkNumber != null){
            number =  checkNumber.getChannelCode() + 1;
        }
        sysChannel.setChannelCode(number);
        sysChannel.setCreateTime(DateUtils.getNowDate());
        sysChannel.setDelFlag(DelFlagStatus.LIVE.getCode());
        return sysChannelMapper.insertSysChannel(sysChannel);
    }

    /**
     * 修改通道
     *
     * @param sysChannel 通道
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysChannel(SysChannel sysChannel)
    {
        sysChannel.setUpdateTime(DateUtils.getNowDate());
        return sysChannelMapper.updateSysChannel(sysChannel);
    }

    /**
     * 删除通道对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysChannelByIds(String ids)
    {
        sysUserChannelMapper.deleteSysUserChannelByChannelId(Convert.toStrArray(ids));
        return sysChannelMapper.deleteSysChannelByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除通道信息
     *
     * @param channelId 通道ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysChannelById(Long channelId)
    {
        return sysChannelMapper.deleteSysChannelById(channelId);
    }

    /**
     * 获取用户端通道
     * @return
     */
    @Override
    public List<SysChannel> selectUserSysChannel() {
        return sysChannelMapper.selectUserSysChannel();
    }
}
